using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace StudentManagementApplication
{
    public partial class FrmCourse : Form
    {
        List<Entities.Course> courseList = new List<Entities.Course>();
        List<Entities.Student> studentList = new List<Entities.Student>();
        List<Entities.Rollcall> rollcallList = new List<Entities.Rollcall>();
        Business.CourseService courseService = new Business.CourseService();
        Business.StudentService studentService = new Business.StudentService();
        Business.MarksService marksService = new Business.MarksService();
        Business.RollcallService rollcallService = new Business.RollcallService();
        Entities.Course course = null;
        int iIndex = 0;

        public FrmCourse()
        {
            InitializeComponent();
        }

        private void InitializeCourse()
        {
            if (dgvCourse.Rows.Count > 1)
            {
                int iCount = dgvCourse.Rows.Count - 1;
                for (int i = 0; i < iCount; i++)
                {
                    dgvCourse.Rows.RemoveAt(0);
                }
            }
            courseList = courseService.GetAllCourses();
            for (int i = 0; i < courseList.Count; i++)
            {
                string[] temp = { courseList[i].CourseID, courseList[i].CourseName };
                dgvCourse.Rows.Add(temp);
            }
            dgvCourse[0, iIndex].Selected = true;
        }

        private void FindCourseByCourseID(string sCourseID)
        {
            if (dgvCourse.Rows.Count > 1)
            {
                int iCount = dgvCourse.Rows.Count - 1;
                for (int i = 0; i < iCount; i++)
                {
                    dgvCourse.Rows.RemoveAt(0);
                }
            }
            courseList = courseService.GetAllCourses();
            for (int i = 0; i < courseList.Count; i++)
            {
                if (courseList[i].CourseID.ToLower().Contains(sCourseID))
                {
                    string[] temp = { courseList[i].CourseID, courseList[i].CourseName };
                    dgvCourse.Rows.Add(temp);
                }
            }
            iIndex = 0;
            dgvCourse[0, iIndex].Selected = true;
            if(dgvCourse.Rows.Count > 1 ) FindCourse(dgvCourse.Rows[iIndex].Cells[0].Value.ToString());
        }

        private void FindCourse(string sCourseID)
        {
            course = new Entities.Course();
            course = courseService.GetCourseByCourseID(sCourseID);
            txtCourseID.Text = course.CourseID;
            txtCourseName.Text = course.CourseName;
        }

        private bool Test()
        {
            bool bResult = true;
            string sInformation = string.Empty;
            if (txtCourseName.Text == string.Empty)
                sInformation += "CourseName is empty" + "\n";
            if (sInformation != string.Empty)
            {
                MessageBox.Show(sInformation, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                bResult = false;
            }
            return bResult;
        }

        private void UpdateCourse()
        {
            if (dgvCourse.CurrentRow.Index >= 0 && Test())
            {
                course.CourseID = txtCourseID.Text;
                course.CourseName = txtCourseName.Text;
                courseService.UpdateCourse(course);
                InitializeCourse();
            }
        }

        private void InsertCourse()
        {
            if (Test())
            {
                course = new Entities.Course();
                course.CourseName = txtCourseName.Text;
                courseService.InsertCourse(course);
                iIndex = dgvCourse.Rows.Count - 1;
                InitializeCourse();
            }
        }

        private void DeleteCourse()
        {
            if (dgvCourse.CurrentRow.Index >= 0)
            {
                try
                {
                    courseService.DeleteCourse(course.CourseID);
                    if (iIndex > 0) iIndex--;
                    else iIndex = 0;
                    InitializeCourse();
                    FindCourse(dgvCourse.Rows[iIndex].Cells[0].Value.ToString());
                }
                catch
                {
                    MessageBox.Show("You can't delete this course because it have references to some classes", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }

        private void RefreshData()
        {
            txtCourseID.Text = string.Empty;
            txtCourseName.Text = string.Empty;
        }

        private void FrmCourse_Load(object sender, EventArgs e)
        {
            InitializeCourse();
        }

        private void rdoUpdate_CheckedChanged(object sender, EventArgs e)
        {
            RefreshData();
            txtCourseID.Enabled = true;
            btnInsert.Enabled = false;
            pictureBox1.Enabled = true;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
            FindCourse(dgvCourse.Rows[iIndex].Cells[0].Value.ToString());
        }

        private void rdoInsert_CheckedChanged(object sender, EventArgs e)
        {
            RefreshData(); 
            txtCourseID.Enabled = false;
            btnInsert.Enabled = true;
            pictureBox1.Enabled = false;
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            FindCourseByCourseID(txtCourseID.Text.ToLower());
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            InsertCourse();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (course != null)
                UpdateCourse();
            else
                MessageBox.Show("You should choose the course which you want to update!");
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (course != null)
            {
                if (MessageBox.Show("Do you really want to delete this file?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
                {
                    DeleteCourse();
                }
            }
            else
                MessageBox.Show("You should choose the course which you want to delete!");
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvCourse_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvCourse.CurrentRow.Index < dgvCourse.Rows.Count - 1)
            {
                iIndex = dgvCourse.CurrentRow.Index;
                string sCourseID = dgvCourse.CurrentRow.Cells[0].Value.ToString();
                if (rdoUpdate.Checked)
                {
                    FindCourse(sCourseID);
                }
            }
        }
    }
}